<?php
/*
	
*/
defined('BY_JYA') or exit('Access Denied');
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$allitemgame=$this->allitemgame;
if($_RQ['op']=='main'){
	if(!$_RQ['id']){
		exi('请选择活动！','error');
	}
	$this->session('pid',$_RQ['id']);
	$item=pdo_get('jy_merch_game_item',array('id'=>$_RQ['id'],'uniacid'=>$_SESSION['uniacid']));
	$this->model('member')->up_fromuser($item['id'],'',$_RQ['fromuser'],$item['merchid']);
	$item['logo']=tomedia($item['logo']);
	$item['gameset']=json_decode($item['gameset'],true);
	$item['canyufileds']=json_decode($item['canyufileds'],true);
	$item['typename']=$allitemgame[$item['gamename']]['title'];
	$item['unitname']=$allitemgame[$item['gamename']]['unit'];
	if(strpos($item['gamename'],'tool_') !== false){
		$item['isdiyform']=1;
	}elseif(strpos($item['gamename'],'game_') !== false){
		$item['isdiygoods']=1;
	}
	if($item['onlinetime']<time()){
		$lasttime=0;
		$item['status']=2;
		$item['statustitle']='已结束';
	}else{
		$lasttime=$item['onlinetime']-time();
		$item['status']=1;
		$item['statustitle']='进行中';
	}
	$item['orderfee']=pdo_getsum('jy_merch_game_order',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id']),'fee');
	$item['sendtypetitle']=$this->sendtypes[$item['sendtype']]?:'上门自提';
    $item['lasttime']=$lasttime;
	$return['item']=$item;
	$return['item']['rest_time']=model('jy_weishop/model')->date($lasttime);
	$return['item']['goods']=json_decode($return['item']['goods'],true);
	foreach($return['item']['goods'] as $key=>&$gv){
		if(!$return['cartnum']){
			$gv['cartnum']=1;
			$return['totalprice']=$gv['price'];
			$return['cartnum']=1;
		}else{
			$gv['cartnum']=0;
		}
		$gv['lastnum']=$gv['total'];
		$gv['isbuynum']=pdo_getsum('jy_merch_game_order_goods',array('ispay'=>1,'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'goodskey'=>$key),'total');
		$gv['mybuynum']=pdo_getsum('jy_merch_game_order_goods',array('ispay'=>1,'mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'goodskey'=>$key),'total');
		if($gv['isbuynum']){
			if(!$gv['total']){
				$gv['percent']=-1;
			}else{
				$total=$gv['total']?:100;
				$gv['lastnum']=$total-$gv['isbuynum'];
				$gv['percent']=intval(100*$gv['isbuynum']/$total);
			}
		}
		if($gv['buynum']){
			$gv['canbuynum']=$gv['buynum']-$gv['mybuynum'];
		}
		if(!$gv['images']){
			$gv['images'][0]=$gv['image']?:'../core/resource/images/nopic.jpg';
		}
		$gv['image']=tomedia($gv['image']);
	}
	$return['item']['price']=price($return['item']['goods'][0]['price']);
	$return['item']['content']=json_decode($return['item']['content'],true);
	foreach($return['item']['content'] as &$cv){
		if($cv['type']==1 || $cv['type']==10){
			if(!$_SESSION['wxapp_id'] || $_SESSION['fromh5wxapp']){
				$cv['data']=str_replace("\n","</br>",$cv['data']);
			}
		}
	}
	$return['item']['desc']=$return['item']['content'][0]['data'];
	$return['item']['diyform']=json_decode($return['item']['diyform'],true);
	if($return['item']['mid']==$_SESSION['uid']){
		$return['item']['ismy']=1;
	}
	$params=array('pid'=>$_RQ['id']);
	$return['params']=$params;
	$return['lognum']=pdo_count('jy_merch_game_order',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$_RQ['id']),'id');
	$return['msgnum']=pdo_count('jy_merch_game_item_msg',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$_RQ['id']),'id');
	$return['merch']=pdo_get('jy_merch_user',array('mid'=>$return['item']['mid'],'uniacid'=>$_SESSION['uniacid']),array('merchname','logo','id'));
	$return['merch']['logo']=tomedia($return['merch']['logo']);
	$ids=pdo_getall('jy_merch_game_item',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$return['item']['mid']),array('id'),'id');
	$return['merch']['gamenum']=count($ids);
	if($ids){
		$ids=array_keys($ids);
	}else{
		$ids=-1;
	}
	$return['merch']['likes']=pdo_count('jy_merch_game_item_like',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$ids),'id');
	$return['islike'] = pdo_count('jy_merch_game_item_like',array('uniacid'=>$_SESSION['uniacid'],'pid'=>$item['id'],'mid'=>$_SESSION['uid']));
	//添加一次浏览次数
	if($_RQ['fromuser']){
		pdo_update('jy_merch_game_item',array('likes +='=>1,'looks +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}else{
		pdo_update('jy_merch_game_item',array('looks +='=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$_RQ['id']));
	}
	$this->model('model')->upitemlook($_RQ['id']);
	$link=custom_url('jy_quanhuo/item//id='.$_RQ['id'].'&fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true);
	if($item['share_set']==2 && !$return['item']['ismy']){
		$share_title='只有发布者可以转发分享，本次给你分享到平台首页！';
		$link=custom_url('jy_quanhuo/index//fromuser='.$_SESSION['uid'].'&current_plugin='.$_SESSION['current_plugin'],true);
	}
	$xuni_mids=$this->setting['xuni_mids'];
	if(strpos($xuni_mids,';'.$_SESSION['uid'].';')!==false){
		$return['is_xuni']=1;
	}
	$return['sharedata']=array(
			'title'=>$share_title?:'['.$item['typename'].']'.$item['title'],
			'desc'=>$return['item']['desc']?:'商家节假日 老带新 裂变引流 成交复购必备工具',
			'link'=>$link,
			'imgUrl'=>ATTACHMENT_URL.$return['item']['logo'],
	);
	set_html('title',$return['item']['title']);
	exi($return);
}
if($_RQ['op']=='getlog'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>$_RQ['pid']);
	$pindex = max(1, intval($_RQ['page']));
	$psize = 30;
	$list=pdo_getslice('jy_merch_game_order',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$lasttime=TIMESTAMP-$item['createtime'];
			$item['lasttime']=$lasttime;
			if($lasttime>24*3600){
				$item['days']=intval($lasttime/(24*3600)).'天前';
			}elseif($lasttime>=3600){
				$item['days']=intval($lasttime/3600).'小时前';
			}else{
				$item['days']=intval($lasttime/60).'分钟前';
			}
			$item['createtime']=date('Y-m-d',$item['createtime']);
			$item['member'] = pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','avatar','nickname'));
			$item['member']['avatar']=tomedia($item['member']['avatar']);
			$item['member']['nickname']=$item['member']['nickname']?cfc('tools')->substr($item['member']['nickname'],0,1).'***':'无';
			$item['goods']=json_decode($item['goods'],true);
		}
	}	
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='getmsg'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'pid'=>$_RQ['pid']);
	if($_RQ['gamename']){
		$params['gamename']=$_RQ['gamename'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_merch_game_item_msg',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$lasttime=TIMESTAMP-$item['createtime'];
			$item['lasttime']=$lasttime;
			if($lasttime>24*3600){
				$item['days']=intval($lasttime/(24*3600)).'天前';
			}elseif($lasttime>=3600){
				$item['days']=intval($lasttime/3600).'小时前';
			}else{
				$item['days']=intval($lasttime/60).'分钟前';
			}
			$item['createtime']=date('Y-m-d',$item['createtime']);
			$item['member'] = pdo_get('core_members',array('id'=>$item['mid'],'uniacid'=>$_SESSION['uniacid']),array('credit1','credit2','mobile','avatar','nickname'));
			$item['member']['avatar']=tomedia($item['member']['avatar']);
			$item['member']['nickname']=$item['member']['nickname']?cfc('tools')->substr($item['member']['nickname'],0,1).'***':'无';
		}
	}	
	$return['list']=$list;
	exi($return);
}
if($_RQ['op']=='setmsg'){
	$pid = $this->session('pid');
	$postdata=get_postdata();
	$checked = ($basic['msg_shenhe'] || $basic['open_wxappsh'])?0:1;
	$data=array(
		'title'=>trim($postdata['msg']),
		'images'=>ijson_encode($postdata['images']),
		'mid'=>$_SESSION['uid'],
		'pid'=>$pid,
		'uniacid'=>$_SESSION['uniacid'],
		'checked'=>$checked,
		'createtime'=>time(),
	);
	pdo_insert('jy_merch_game_item_msg',$data);
	if($checked){
		exi('评论成功！');
	}else{
		exi('评论成功！请等待审核');
	}
}
if($_RQ['op']=='setend'){
	$pid = $this->session('pid');
	$update=array(
		'onlinetime'=>time()-100,
	);
	pdo_update('jy_merch_game_item',$update,array('id'=>$pid));
	exi('结束活动成功！');
}
if($_RQ['op']=='check_buy'){
	$post=get_postdata();
	$total=0;
	$total_price=0.00;
	foreach($post['goods'] as &$ov){
		$total+=$ov['cartnum'];
		$total_price+=$ov['cartnum']*$ov['price'];
	}
	if($total<=0){
		exi('请选择商品！','error');
	}
	$order = array(
		'merchid'=>$post['merchid'],
		'pid'=>$post['id'],
		'gamename'=>$post['gamename'],
		'goods'=>$post['goods'],
		'canyuform'=>$post['canyuform'],
		'sendtype'=>$post['sendtype'],
		'typename'=>$post['typename'],
	);
	$this->session('order',$order);
	exi('','','/create/');
}
if($_RQ['op']=='diyform'){
	$post=get_postdata();
	$order = array(
		'merchid'=>$post['merchid'],
		'pid'=>$post['id'],
		'gamename'=>$post['gamename'],
		'canyuform'=>ijson_encode($post['canyufileds']),
		'diyform'=>ijson_encode($post['diyform']),
		'typename'=>$post['typename'],
		'status'=>8,
	);
	$this->model('order')->create($order);
	exi('提交成功！');
}